package com.uucrazy.datacneter.m.sae.client.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.uucrazy.datacenter.client.DataCenterClent;
import com.uucrazy.datacenter.data.BaseUser;
import com.uucrazy.datacenter.data.Entry;
import com.uucrazy.datacenter.data.FileEntry;
import com.uucrazy.datacenter.data.FolderEntry;
import com.uucrazy.datacenter.data.User;
import com.uucrazy.datacenter.data.User.Attribute;

public class SAEDataCenterClent implements DataCenterClent
{
	
	private static String ROOT_SITE = "http://2.uucrazy.sinaapp.com";
	private static String M_DIR = "sqltest.php";
	
	HttpClientHelper helper = new HttpClientHelper();

	@Override
	public Entry getEntry(String id)
	{
		return null;
	}

	@Override
	public List<Entry> getEntrys(Map<String, Object> p)
	{
		List<Entry> entrys = new ArrayList<Entry>();
		try
		{
			Map<String, String> params = new HashMap<String,String>();
			params.put("puuid", (String)p.get("parentId"));
			String jsonString = helper.getString(ROOT_SITE + "/" + M_DIR, params);
			System.out.println(jsonString);
			//List<List<String>> lls = (List<List<String>>) JSONUtil.deserialize(jsonString);
			List<JSONArray> lls= JSON.parseArray(jsonString, JSONArray.class);
			for (JSONArray jsonArray : lls)
			{
				//"UUID","PARENT_UUID","NAME","LABEL","CREATE_TIME","MODIFY_TIME","CONTENTTYPE","URL"
				String m = jsonArray.getString(6);
				if (m==null || "".equals(m) || "0".equals(m)) // 文件夹
				{
					FolderEntry folderEntry = new FolderEntry();
					folderEntry.setName(jsonArray.getString(2));
					folderEntry.setId(jsonArray.getString(0));
					folderEntry.setParentId(jsonArray.getString(1));
					folderEntry.setCover(jsonArray.getString(7));
					entrys.add(folderEntry);
				}
				else
				{
					FileEntry fileEntry = new FileEntry();
					fileEntry.setName(jsonArray.getString(2));
					fileEntry.setId(jsonArray.getString(0));
					fileEntry.setParentId(jsonArray.getString(1));
					fileEntry.setUrl(jsonArray.getString(7));
					entrys.add(fileEntry);
				}
			}
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return entrys;
	}

	@Override
	public User getUser()
	{
		BaseUser user = new BaseUser();
		user.setId("Card2011");
		user.setAvatar("http://tp4.sinaimg.cn/2009656823/50/1299463880/1");
		user.setName("杰伦有点拽");
		Attribute attr = new Attribute();
		attr.setKey("compay");
		attr.setName("公司");
		attr.setValue("优果有限公司");
		user.addAttribute(attr);
		Attribute attr2 = new Attribute();
		attr2.setKey("A2");
		attr2.setName("A2");
		attr2.setValue("A2");
		user.addAttribute(attr2);
		return user;
	}

}
